package com.controller;

import com.base.AjaxResult;
import com.base.BaseController;
import com.bean.LogintokenContent;
import com.bean.LogintokenSource;
import com.service.LogintokenService;
import com.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("logintoken")
public class LoginTokenController extends BaseController {

    @Autowired
    private JWTUtil jwtUtil;

    @Autowired
    private LogintokenService logintokenService;

    @PostMapping("list")
    public AjaxResult list(@RequestBody LogintokenSource logintokenSource){
        String userid = jwtUtil.getUserId();
        logintokenSource.setUserid(userid);
        List<LogintokenSource> list = logintokenService.listLogintokenSource(logintokenSource);
        return success("查询成功",list);
    }

    @PostMapping("add")
    public AjaxResult add(@RequestBody LogintokenSource logintokenSource){
        String userid = jwtUtil.getUserId();
        logintokenSource.setUserid(userid);
        logintokenService.addLogintokenSource(logintokenSource);
        LogintokenContent logintokenContent = logintokenSource.getLogintokenContent();
        logintokenContent.setSid(logintokenSource.getId());
        logintokenService.addLogintokenContent(logintokenContent);
        return success("新增成功");
    }

    @PostMapping("get")
    public AjaxResult get(@RequestBody LogintokenSource logintokenSource){
        logintokenSource = logintokenService.getLogintoken(logintokenSource);
        return success("获得成功",logintokenSource);
    }

    @PostMapping("update")
    public AjaxResult update(@RequestBody LogintokenSource logintokenSource){
        String userid = jwtUtil.getUserId();
        logintokenSource.setUserid(userid);
        logintokenService.updateLogintokenSource(logintokenSource);
        LogintokenContent logintokenContent = logintokenSource.getLogintokenContent();
        logintokenContent.setSid(logintokenSource.getId());
        logintokenService.updateLogintokenContent(logintokenContent);
        return success("更新成功");
    }

    @PostMapping("del")
    public AjaxResult del(@RequestBody LogintokenSource logintokenSource){
        logintokenService.delLogintoken(logintokenSource);
        return success("删除成功");
    }
}
